package com.woniuxy.hrms.dao.impl;

import com.woniuxy.hrms.dao.AccountDao;
import com.woniuxy.hrms.entity.Account;
import com.woniuxy.hrms.util.DBUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @program: hrms
 * @description:
 * @create: 2024-08-09 15:30
 **/
public class AccountDaoImpl implements AccountDao {
    Connection connection;
    PreparedStatement ps;
    ResultSet rs;
    String sql;

    @Override
    public Account queryByNo(String accountNo) throws SQLException {
        Account account = null;
        connection = DBUtil.getCon();//通过工具类得到连接对象
        sql = "select * from account where account_no=?";
        ps = connection.prepareStatement(sql);
        ps.setString(1, accountNo);
        rs = ps.executeQuery();//执行查询并返回一个ResultSet对象,有一个指针指向到第一条之前
        if (rs.next()) {
            int id = rs.getInt(1);
            String accountNo2 = rs.getString(2);
            String accountName = rs.getString(3);
            BigDecimal balance = rs.getBigDecimal(4);
            account = new Account(id, accountNo2, accountName, balance);
        }
        DBUtil.release(connection, ps, rs);//释放连接对象等信息
        return account;
    }

    @Override
    public int update(Account account,Connection conn) throws SQLException {
        connection = conn;//通过工具类得到连接对象//传进来的连接对象
        sql = "update account set balance=? where account_no=?";
        ps = connection.prepareStatement(sql);
        ps.setBigDecimal(1, account.getBalance());
        ps.setString(2, account.getAccountNo());
        int i = ps.executeUpdate();
        //DBUtil.release(connection, ps, rs);//释放连接对象等信息
        return i;
    }
}